package com.woniuxy.woniubook.controller;


import com.woniuxy.woniubook.entity.Address;
import com.woniuxy.woniubook.entity.User;
import com.woniuxy.woniubook.service.AddressService;
import com.woniuxy.woniubook.service.UserService;
import com.woniuxy.woniubook.uitl.CookieUtil;
import com.woniuxy.woniubook.uitl.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wugd
 * @since 2022-11-16 02:13:10
 */
@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    AddressService addressService ;
    @Autowired
    UserService userService ;

    @RequestMapping("/list")
    public List<Address> list(HttpServletRequest request){
        // 获取 用户的ID
        String user_token = CookieUtil.getToken(request, "user_token");
        String account = JwtUtils.getAccount(user_token);
        //查询用户对象
        User user = userService.getUserByName(account);

        List<Address> list = addressService.getAddressByUser(user.getId());
        return list ;
    }

    @RequestMapping("/addAddress")
    public String addAddress(HttpServletRequest request, Address address){
        // 获取 用户的ID
        String user_token = CookieUtil.getToken(request, "user_token");
        String account = JwtUtils.getAccount(user_token);
        //查询用户对象
        User user = userService.getUserByName(account);
        address.setStatus("1");
        address.setUserId(user.getId());
        addressService.add(address);
        return "ok" ;
    }



}

